package com.tangdi.mkfile.gzdwtzs;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.RectangleReadOnly;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.tangdi.ReportFileInfo;
import com.tangdi.Utils;
import com.tangdi.Bean.CZGZ_GZLB;
import com.tangdi.Bean.CZGZ_SJGZ;
import com.tangdi.Bean.CZGZ_ZGGZMX;
import com.tangdi.Bean.DW_DM_MC;
import com.tangdi.Bean.XM_BM_XM_MC;
import com.tangdi.Dao.impl.GongziMXImpl;

/**
 * @author yang
 *
 */
public class CreateDWGZPDF_TZS {
	private Logger logger = Logger.getLogger(CreateDWGZPDF_TZS.class);
	private String pdf_url_root = Utils.getFilePath("root_path");
	private String folder_Name = ReportFileInfo.FOLDER_NAME_GZDWTZS.getName();
	private String file_Type = "pdf";
	private String file_name_type = ReportFileInfo.FILE_NAME_GZDWTZS.getName();
	
	private String title_content = ReportFileInfo.FILE_NAME_GZDWTZS_TITLE.getName();
	private String transfer_content_01 = ReportFileInfo.FILE_NAME_GZDWTZS_CONTENT01.getName();
	private String transfer_content_02 = ReportFileInfo.FILE_NAME_GZDWTZS_CONTENT02.getName();
	private String transfer_content_03 = ReportFileInfo.FILE_NAME_GZDWTZS_CONTENT03.getName();
	private String transfer_content_030 = ReportFileInfo.FILE_NAME_GZDWTZS_CONTENT030.getName();
	private String transfer_content_04 = ReportFileInfo.FILE_NAME_GZDWTZS_CONTENT04.getName();
	
	private DecimalFormat decimalFormat = new DecimalFormat("#0.00");
	private Font FontChinese;
	
	public int createPDF(Map<String,List<XM_BM_XM_MC>> map_bm_mc_list,List<DW_DM_MC> listDW_DM_MC,List<CZGZ_GZLB> list_gz_lb,String month) {
		StringBuffer sql_select001 = null;
		StringBuffer sql_select002 = null;
		String sql_select_count001 = null;
		String sql_select_count002 = null;
		int initSize = 0;
		float width = 22f;
		int gz_colNumMax = 0;
				
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    try {
	        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
	        //BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
	        Font FontChinese_titleContent = new Font(bfChinese, 12, Font.NORMAL);
	        FontChinese = new Font(bfChinese, 10, Font.NORMAL);

			GongziMXImpl gzmx = new GongziMXImpl();
			//listDWCOUNT.size()
			Document document = null;
			PdfPTable table = null;
			String pdf_url = "";
			
			String GZ_LB_DM001 = "5";//在职
			String work_status_cde01 = "01";//在职
			String work_status_cde02 = "04";//病休

			String GZ_LB_DM002 = "13";//退休
			String work_status_cde03 = "05";//离休
			String work_status_cde04 = "06";//退休
			
			//listDW_DM_MC.size()
			for(int i = 0; i < listDW_DM_MC.size(); i++){
				logger.info(listDW_DM_MC.get(i).getDM() + "--" + listDW_DM_MC.get(i).getMC());
				//String rs_Count001 = "0";
				String rs_WorkStatus01 = "0";
				String rs_WorkStatus02 = "0";
				//String rs_Count002 = "0";
				String rs_WorkStatus03 = "0";
				String rs_WorkStatus04 = "0";
				String km_Info001 = "";
				String km_MC001 = "";
				String km_DM001 = "";
				String km_Info002 = "";
				String km_MC002 = "";
				String km_DM002 = "";
				
				DW_DM_MC dw_dm_mc = listDW_DM_MC.get(i);//获取单位代码、名称
				pdf_url = pdf_url_root + "/" + month + "/" + file_Type + "/" + folder_Name + "/" + month + "_" + file_name_type + "_" + dw_dm_mc.getDM() + "_" + dw_dm_mc.getMC() + ".pdf";
				//document = new Document(PageSize.A3,0,0,0,0);
				//document = new Document(new RectangleReadOnly(1020F, 790F),0,0,0,0);
				document = new Document(new RectangleReadOnly(595F, 790F),0,0,0,0);
				//在填写文件路径时，一定要写上具体的文件名称（xx.txt），否则会出现拒绝访问。
				File file = new File(pdf_url);
				if(!file.exists()){
				    //先得到文件的上级目录，并创建上级目录，在创建文件
					//file.getParentFile().mkdir();
					file.getParentFile().mkdirs();
					file.createNewFile();
				}
		        PdfWriter.getInstance(document, new FileOutputStream(pdf_url));
				PdfWriter.getInstance(document, baos);
				document.open();
			
								
				//单位  在职员工  工资汇总 start
				sql_select001 = new StringBuffer("SELECT ");
				List<XM_BM_XM_MC> list_BM_MC001 = map_bm_mc_list.get(GZ_LB_DM001);
				
				for(int m = 0; m < list_BM_MC001.size(); m++){
					sql_select001.append("coalesce(TO_CHAR(to_number(SUM("+ list_BM_MC001.get(m).getXM_BM() +")),'9999999999990.00'),'0.00') AS " +list_BM_MC001.get(m).getXM_BM() + ",");
				}
				sql_select_count001 = sql_select001.substring(0, sql_select001.length() - 1);
				CZGZ_ZGGZMX gzDW_Count_WorkStatus_01 = new CZGZ_ZGGZMX();
				CZGZ_ZGGZMX gzDW_Count_WorkStatus_02 = new CZGZ_ZGGZMX();
				gzDW_Count_WorkStatus_01 = gzmx.getCountGZByLB_WorkCode(month, dw_dm_mc.getDM(), GZ_LB_DM001, list_BM_MC001, sql_select_count001,work_status_cde01);
				gzDW_Count_WorkStatus_02 = gzmx.getCountGZByLB_WorkCode(month, dw_dm_mc.getDM(), GZ_LB_DM001, list_BM_MC001, sql_select_count001,work_status_cde02);
				//rs_Count001 = gzmx.getDWRSCount_ByLB(month, "SELECT COUNT(1) FROM CZGZ_ZGGZMX WHERE YF = '" + month +"' AND DW_DM = '" + dw_dm_mc.getDM() + "' AND GZ_LB_DM = '"+ GZ_LB_DM001 +"'");
				rs_WorkStatus01 = gzmx.getDWRSCount_ByLB(month, "SELECT COUNT(1) FROM CZGZ_ZGGZMX WHERE YF = '" + month +"' AND DW_DM = '" + dw_dm_mc.getDM() + "' AND GZ_LB_DM = '"+ GZ_LB_DM001 +"'" + " AND WORK_STATUS_CDE = '"+ work_status_cde01 +"'");
				rs_WorkStatus02 = gzmx.getDWRSCount_ByLB(month, "SELECT COUNT(1) FROM CZGZ_ZGGZMX WHERE YF = '" + month +"' AND DW_DM = '" + dw_dm_mc.getDM() + "' AND GZ_LB_DM = '"+ GZ_LB_DM001 +"'" + " AND WORK_STATUS_CDE = '"+ work_status_cde02 +"'");
				km_Info001 = gzmx.getDWRSCount_ByLB(month, "SELECT CONCAT(KM_MC,',',KM_DM) AS KM_MC_DM FROM CZGZ_ZGGZMX WHERE YF = '" + month +"' AND DW_DM = '" + dw_dm_mc.getDM() + "' AND GZ_LB_DM = '"+ GZ_LB_DM001 +"'  limit 1 " );
				logger.info("km_Info001: " + km_Info001);
				if(km_Info001 != null && !km_Info001.equals("0")){
					if(km_Info001.indexOf(",") != 0){
						km_MC001 = km_Info001.substring(0, km_Info001.indexOf(","));
					}
					if(km_Info001.indexOf(",") != km_Info001.length()){
						km_DM001 = km_Info001.substring(km_Info001.indexOf(",") + 1, km_Info001.length());
					}
				}
				//单位  在职员工  工资汇总 end
				
				//单位  离职员工  工资汇总 start
				sql_select002 = new StringBuffer("SELECT ");
				List<XM_BM_XM_MC> list_BM_MC002 = map_bm_mc_list.get(GZ_LB_DM002);			
				for(int n = 0; n < list_BM_MC002.size(); n++){
					sql_select002.append("coalesce(TO_CHAR(to_number(SUM("+ list_BM_MC002.get(n).getXM_BM() +")),'9999999999990.00'),'0.00') AS " +list_BM_MC002.get(n).getXM_BM() + ",");
				}
				sql_select_count002 = sql_select002.substring(0, sql_select002.length() - 1);
				//按工资类别、单位代码，汇总工资明细各科目的值  职工状态码
				CZGZ_ZGGZMX gzDW_Count_WorkStatus_03 = new CZGZ_ZGGZMX();
				CZGZ_ZGGZMX gzDW_Count_WorkStatus_04 = new CZGZ_ZGGZMX();
				gzDW_Count_WorkStatus_03 = gzmx.getCountGZByLB_WorkCode(month, dw_dm_mc.getDM(), GZ_LB_DM002, list_BM_MC002, sql_select_count002,work_status_cde03);
				gzDW_Count_WorkStatus_04 = gzmx.getCountGZByLB_WorkCode(month, dw_dm_mc.getDM(), GZ_LB_DM002, list_BM_MC002, sql_select_count002,work_status_cde04);
				//rs_Count002 = gzmx.getDWRSCount_ByLB(month, "SELECT COUNT(1) FROM CZGZ_ZGGZMX WHERE YF = '" + month +"' AND DW_DM = '" + dw_dm_mc.getDM() + "' AND GZ_LB_DM = '"+ GZ_LB_DM002 +"'" );
				rs_WorkStatus03 = gzmx.getDWRSCount_ByLB(month, "SELECT COUNT(1) FROM CZGZ_ZGGZMX WHERE YF = '" + month +"' AND DW_DM = '" + dw_dm_mc.getDM() + "' AND GZ_LB_DM = '"+ GZ_LB_DM002 +"'" + " AND WORK_STATUS_CDE = '"+ work_status_cde03 +"'");
				rs_WorkStatus04 = gzmx.getDWRSCount_ByLB(month, "SELECT COUNT(1) FROM CZGZ_ZGGZMX WHERE YF = '" + month +"' AND DW_DM = '" + dw_dm_mc.getDM() + "' AND GZ_LB_DM = '"+ GZ_LB_DM002 +"'" + " AND WORK_STATUS_CDE = '"+ work_status_cde04 +"'");
				
				km_Info002 = gzmx.getDWRSCount_ByLB(month, "SELECT CONCAT(KM_MC,',',KM_DM) AS KM_MC_DM FROM CZGZ_ZGGZMX WHERE YF = '" + month +"' AND DW_DM = '" + dw_dm_mc.getDM() + "' AND GZ_LB_DM = '"+ GZ_LB_DM002 +"'  limit 1 " );
				if(km_Info002 != null && !km_Info002.equals("0")){
					if(km_Info002.indexOf(",") != 0){
						km_MC002 = km_Info002.substring(0, km_Info002.indexOf(","));	
					}
					if(km_Info002.indexOf(",") != km_Info002.length()){
						km_DM002 = km_Info002.substring(km_Info002.indexOf(",") + 1, km_Info002.length());
					}
				}
				//单位  离职员工  工资汇总 end
				
				//生成表
				initSize = 11;
				float[] cellWidths = new float[initSize];
				for(int a = 0; a < initSize; a++){
					cellWidths[a] = width;
			    }
				cellWidths[0] = 10f;
				cellWidths[1] = 15f;
				cellWidths[5] = 2f;
				cellWidths[6] = 10f;
				cellWidths[7] = 15f;
								
				table = new PdfPTable(cellWidths);
				//***************表连续不分页，至关重要的作用 
				//table.setKeepTogether(true);
				
				table.setTotalWidth((initSize - 2) * width);
				//table.setSpacingBefore(0f);
				//table.getDefaultCell().setMinimumHeight(35);
				table.addCell(getCell_noBorder_title(title_content, initSize, 1, Element.ALIGN_CENTER, Element.ALIGN_BOTTOM, new Font(bfChinese, 18, Font.BOLD)));
				table.addCell(getCell_noBorder(" ", initSize, 1, Element.ALIGN_LEFT, Element.ALIGN_CENTER,FontChinese_titleContent));
				//
				table.addCell(getCell_noBorder(dw_dm_mc.getMC().toString() + "：", 8, 1, Element.ALIGN_LEFT, Element.ALIGN_CENTER,FontChinese_titleContent));
				table.addCell(getCell_noBorder("编号：JH" + month + dw_dm_mc.getDM(), 3, 1, Element.ALIGN_RIGHT, Element.ALIGN_CENTER,FontChinese_titleContent));
				//
				table.addCell(getCell_noBorder(transfer_content_01 + month.substring(0, 4) + " 年 " + month.substring(4, 6)+ " 月" + 
							                   transfer_content_02 + 
				                    		   getStringDoubleAdd(gzDW_Count_WorkStatus_01.getSF_TXN_AMT(),
				                    				   gzDW_Count_WorkStatus_02.getSF_TXN_AMT(),
				                    				   gzDW_Count_WorkStatus_01.getB009() == null ? "0.00" : gzDW_Count_WorkStatus_01.getB009(),
				                    				   gzDW_Count_WorkStatus_02.getB009() == null ? "0.00" : gzDW_Count_WorkStatus_02.getB009(),
													   gzDW_Count_WorkStatus_03.getSF_TXN_AMT(),
				                                       gzDW_Count_WorkStatus_04.getSF_TXN_AMT()
				                    				   ) +
						                      transfer_content_03,
											  initSize,1, Element.BODY, Element.ALIGN_BOTTOM,FontChinese_titleContent));
				table.addCell(getCell_noBorder(
						  transfer_content_030 +
						  getStringDoubleAdd(gzDW_Count_WorkStatus_01.getB009(), gzDW_Count_WorkStatus_02.getB009()) +
	                      transfer_content_04,
						  initSize,1, Element.BODY, Element.ALIGN_BOTTOM,FontChinese_titleContent));
				table.addCell(getCell_noBorder(" ", initSize, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese_titleContent));
				
				//表头
				table.addCell(getCell_noBorder("工资类别：机关", 5, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell_noBorder("", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell_noBorder("工资类别：离退休", 5, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				//
				table.addCell(getCell("项目", 2, 2, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("人员类别", 3, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell_noBorder("", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("项目", 2, 2, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("人员类别", 3, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				
                //
				table.addCell(getCell("在职", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("离岗退养", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("合计", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell_noBorder("", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("离休", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("退休（职）", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("合计", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				//
				table.addCell(getCell("人    数", 2, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(rs_WorkStatus01, 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(rs_WorkStatus02, 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(getStringIntegerAdd(rs_WorkStatus01, rs_WorkStatus02), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell_noBorder("", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("人    数", 2, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(rs_WorkStatus03, 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(rs_WorkStatus04, 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(getStringIntegerAdd(rs_WorkStatus03, rs_WorkStatus04), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				//
				table.addCell(getCell("科目", 1, 2, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("名称", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCellWrap(km_MC001, 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(" ", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(" ", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));

				table.addCell(getCell_noBorder("", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				
				table.addCell(getCell("科目", 1, 2, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("名称", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCellWrap(km_MC002, 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				//System.out.println("km_MC001:" + km_MC001 + " | km_DM001:" + km_DM001 + "km_MC002:" + km_MC002 + " | km_DM002:" + km_DM002);
				table.addCell(getCell(" ", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(" ", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				//
				table.addCell(getCell("代码", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(km_DM001, 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(" ", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(" ", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell_noBorder("", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("代码", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(km_DM002, 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(" ", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(" ", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				
				//
				table.addCell(getCellWrap("资金来源", 1, 2, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("预算内", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_01.getYF_GZ(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_02.getYF_GZ(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(getStringDoubleAdd(gzDW_Count_WorkStatus_01.getYF_GZ(), gzDW_Count_WorkStatus_02.getYF_GZ()),
								      1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));

				table.addCell(getCell_noBorder("", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				
				table.addCell(getCellWrap("资金来源", 1, 2, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("预算内", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_03.getYF_GZ(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_04.getYF_GZ(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(getStringDoubleAdd(gzDW_Count_WorkStatus_03.getYF_GZ(), gzDW_Count_WorkStatus_04.getYF_GZ()),
								      1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				//
				table.addCell(getCell("预算外", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("0.00", 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("0.00", 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("0.00", 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell_noBorder("", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("预算外", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("0.00", 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("0.00", 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("0.00", 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				
				//
				table.addCell(getCell("工资", 1, 3, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("应发", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_01.getYF_GZ(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_02.getYF_GZ(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(getStringDoubleAdd(gzDW_Count_WorkStatus_01.getYF_GZ(), gzDW_Count_WorkStatus_02.getYF_GZ()),
									  1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));

				table.addCell(getCell_noBorder("", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				
				table.addCell(getCell("工资", 1, 3, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("应发", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_03.getYF_GZ(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_04.getYF_GZ(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(getStringDoubleAdd(gzDW_Count_WorkStatus_03.getYF_GZ(), gzDW_Count_WorkStatus_04.getYF_GZ()),
								      1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				//
				table.addCell(getCell("扣款小计", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_01.getDK_XJ(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_02.getDK_XJ(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(getStringDoubleAdd(gzDW_Count_WorkStatus_01.getDK_XJ(), gzDW_Count_WorkStatus_02.getDK_XJ()),
					                  1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell_noBorder("", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("扣款小计", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_03.getDK_XJ(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_04.getDK_XJ(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(getStringDoubleAdd(gzDW_Count_WorkStatus_03.getDK_XJ(), gzDW_Count_WorkStatus_04.getDK_XJ()),
		                  			  1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				
				//
				table.addCell(getCell("实发", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_01.getSF_TXN_AMT(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_02.getSF_TXN_AMT(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(getStringDoubleAdd(gzDW_Count_WorkStatus_01.getSF_TXN_AMT(), gzDW_Count_WorkStatus_02.getSF_TXN_AMT()),
										  1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell_noBorder("", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell("实发", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_03.getSF_TXN_AMT(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(gzDW_Count_WorkStatus_04.getSF_TXN_AMT(), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell(getStringDoubleAdd(gzDW_Count_WorkStatus_03.getSF_TXN_AMT(), gzDW_Count_WorkStatus_04.getSF_TXN_AMT()),
									   1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
                
				gz_colNumMax = list_BM_MC001.size();
				if(gz_colNumMax < list_BM_MC002.size()){
					gz_colNumMax = list_BM_MC002.size();
				}
				for(int k = 0; k < gz_colNumMax - 2; k++){//根据工资类别反射查找工资科目
					if(k >=  (list_BM_MC001.size() - 2)){
						table.addCell(getCell(" ", 2, 1, Element.ALIGN_RIGHT, Element.ALIGN_LEFT,FontChinese));
						table.addCell(getCell(" ", 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
						table.addCell(getCell(" ", 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
						table.addCell(getCell(" ", 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));	
					}else{
						String gz_value_WorkStatus01 = "0.00";
						String gz_value_WorkStatus02 = "0.00";
						if(gzDW_Count_WorkStatus_01 != null){
							gz_value_WorkStatus01 = (String)Utils.getFieldReflect(gzDW_Count_WorkStatus_01,CZGZ_ZGGZMX.class,list_BM_MC001.get(k).getXM_BM());
						}
						if(gzDW_Count_WorkStatus_02 != null){
							gz_value_WorkStatus02 = (String)Utils.getFieldReflect(gzDW_Count_WorkStatus_02,CZGZ_ZGGZMX.class,list_BM_MC001.get(k).getXM_BM());
						}
						
						String xm_mingcheng_001 = list_BM_MC001.get(k).getXM_MC();
						table.addCell(getCell((7 <= xm_mingcheng_001.length() ? xm_mingcheng_001.substring(0, 7) : xm_mingcheng_001), 2, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
						table.addCell(getCell(gz_value_WorkStatus01, 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
						table.addCell(getCell(gz_value_WorkStatus02, 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
						table.addCell(getCell(getStringDoubleAdd(gz_value_WorkStatus01, gz_value_WorkStatus02), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
					}
					
					table.addCell(getCell_noBorder("", 1, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
					
					if( k >= (list_BM_MC002.size() - 2)){
						table.addCell(getCell(" ", 2, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
						table.addCell(getCell(" ", 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
						table.addCell(getCell(" ", 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
						table.addCell(getCell(" ", 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
					}else{
						String gz_value_WorkStatus03 = "0.00";
						String gz_value_WorkStatus04 = "0.00";
						if(gzDW_Count_WorkStatus_03 != null){
							gz_value_WorkStatus03 = (String)Utils.getFieldReflect(gzDW_Count_WorkStatus_03,CZGZ_ZGGZMX.class,list_BM_MC002.get(k).getXM_BM());							
						}
						if(gzDW_Count_WorkStatus_04 != null){
							gz_value_WorkStatus04 = (String)Utils.getFieldReflect(gzDW_Count_WorkStatus_04,CZGZ_ZGGZMX.class,list_BM_MC002.get(k).getXM_BM());							
						}
						
						String xm_mingcheng_002 = list_BM_MC002.get(k).getXM_MC();
						table.addCell(getCell((7 <= xm_mingcheng_002.length() ? xm_mingcheng_002.substring(0, 7) : xm_mingcheng_002), 2, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
						table.addCell(getCell(gz_value_WorkStatus03, 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
						table.addCell(getCell(gz_value_WorkStatus04, 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
						table.addCell(getCell(getStringDoubleAdd(gz_value_WorkStatus03, gz_value_WorkStatus04), 1, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM,FontChinese));
					}
				}
				table.addCell(getCell_noBorder(" ", initSize, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				
				table.addCell(getCell_noBorder("代理银行业务章：", 3, 1, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell_noBorder("负责人：", 2, 1, Element.ALIGN_CENTER, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell_noBorder("经办人：", 3, 1, Element.ALIGN_CENTER, Element.ALIGN_BOTTOM,FontChinese));
				table.addCell(getCell_noBorder("制表日期："+ month.substring(0, 4) + "年" + month.substring(4, 6)+ "月", 3, 1, Element.ALIGN_CENTER, Element.ALIGN_BOTTOM,FontChinese));
				
				table.setWidthPercentage(90); // 设置表格宽度
				//document.add
				document.add(table);
				document.close();
			}
	    } catch (Exception e) {
	    	e.printStackTrace();
	    	return -1;
		}
	    return 0;
	}
	private PdfPCell getCell_noBorder_title(String cellValue, int colspan, int rowSpan, int x_position, int y_position, Font font) {
	    PdfPCell cell = new PdfPCell();
	    try {
	        cell = new PdfPCell(new Phrase(cellValue, font));
	        cell.setRowspan(rowSpan);
	        cell.setColspan(colspan);
	        cell.setFixedHeight(60f);
	        cell.setBorder(0);
	        cell.setHorizontalAlignment(x_position);
	        cell.setVerticalAlignment(y_position);
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return cell;
	}
	private PdfPCell getCell_noBorder(String cellValue, int colspan, int rowSpan, int x_position, int y_position, Font font) {
	    PdfPCell cell = new PdfPCell();
	    try {
	        cell = new PdfPCell(new Phrase(cellValue, font));
	        cell.setRowspan(rowSpan);
	        cell.setColspan(colspan);
	        //cell.setFixedHeight(36.547f);
	        cell.setBorder(0);
	        cell.setHorizontalAlignment(x_position);
	        cell.setVerticalAlignment(y_position);
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return cell;
	}
	
	private PdfPCell getCell(String cellValue, int colspan, int rowSpan, int x_position, int y_position, Font font) {
	    PdfPCell cell = new PdfPCell();
	    try {
	        cell = new PdfPCell(new Phrase(cellValue, font));
	        cell.setRowspan(rowSpan);
	        cell.setColspan(colspan);
	        cell.setNoWrap(true);
	        cell.setUseVariableBorders(true);
	        //cell.setFixedHeight(36.547f);
	        //cell.setBorder(0);
	        cell.setHorizontalAlignment(x_position);
	        cell.setVerticalAlignment(y_position);
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return cell;
	}
	private PdfPCell getCellWrap(String cellValue, int colspan, int rowSpan, int x_position, int y_position, Font font) {
	    PdfPCell cell = new PdfPCell();
	    try {
	        cell = new PdfPCell(new Phrase(cellValue, font));
	        cell.setRowspan(rowSpan);
	        cell.setColspan(colspan);
	        //cell.setFixedHeight(36.547f);
	        //cell.setBorder(0);
	        cell.setHorizontalAlignment(x_position);
	        cell.setVerticalAlignment(y_position);
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return cell;
	}
	
	public String getStringDoubleAdd(String ... values){
		double result = 0;
		for(String value : values){
			result = result + Double.parseDouble(value.trim());
		}
		return decimalFormat.format(result);
	}
	public String getStringIntegerAdd(String value01, String value02){
		return String.valueOf(Integer.parseInt(value01.trim()) +
							  Integer.parseInt(value02.trim()));
	}
	public static void main(String[] args) {
		//年月
		String month = "201905";
		//根据单位编号
		List<DW_DM_MC> listDW_DM_MC=new ArrayList<DW_DM_MC>();
		GongziMXImpl gz_DW_List = new GongziMXImpl();
		listDW_DM_MC = gz_DW_List.getListDW_DM_MC(month);	
		
		//工资类别，科目项信息
		Map<String,List<XM_BM_XM_MC>> map_bm_mc_list = new HashMap<String,List<XM_BM_XM_MC>>();
		
		//工资类别list
		List<CZGZ_GZLB> listlb=new ArrayList<CZGZ_GZLB>();
		GongziMXImpl gzlb = new GongziMXImpl();
		listlb = gzlb.listGZLBReport(month);
		
		//实际工资科目 按工资类别
		List<CZGZ_SJGZ> list_sjgz=new ArrayList<CZGZ_SJGZ>();
		GongziMXImpl sjgz = new GongziMXImpl();
		list_sjgz = sjgz.listSJGZReport(month);	
		
		//组装工资类别 ，实际工资科目项，对应好关系；方便后续使用 list_BM_MC001
		for(int i=0;i<listlb.size();i++){
			List<XM_BM_XM_MC> bm_mc_list = new ArrayList<XM_BM_XM_MC>();
			System.out.println(listlb.get(i).getLB_BM().trim());
			for(int j=0; j<list_sjgz.size(); j++){
				XM_BM_XM_MC bm_mc= new XM_BM_XM_MC();
				if((listlb.get(i).getLB_BM().trim()).equals(list_sjgz.get(j).getGZ_LB().trim())){					
					bm_mc.setXM_BM(Utils.getXM_BM(list_sjgz.get(j).getXM_BM().trim()));					
					bm_mc.setXM_MC(list_sjgz.get(j).getXM_MC().trim());					
					System.out.print(Utils.getXM_BM(list_sjgz.get(j).getXM_BM().trim()) + " | ");	
					bm_mc_list.add(bm_mc);
				}
			}
			System.out.println();
			map_bm_mc_list.put(listlb.get(i).getLB_BM().trim(), bm_mc_list);
		}
		
		CreateDWGZPDF_TZS c = new CreateDWGZPDF_TZS();
	    c.createPDF(map_bm_mc_list,listDW_DM_MC,listlb,month);
	    System.out.println("制作完成");
	}
}
